package com.tz.tzgw.pc.web.dao;

import com.tz.tzgw.pc.web.model.*;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class ZtImagesDao {
    private final JdbcTemplate jdbcTemplate;

    public ZtImagesDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public PageResult<ZtImages> getZtImagesByPageAndCondition(int pageNum, int pageSize, Long setid, Integer status) {
        PageResult<ZtImages> pageResult = new PageResult<>();
        int offset = (pageNum - 1) * pageSize;

        // 构建查询总条数的 SQL
        StringBuilder countSqlBuilder = new StringBuilder("SELECT COUNT(*) FROM tz_zt_images");
        List<Object> countParams = new ArrayList<>();

        // 构建分页查询的 SQL
        StringBuilder querySqlBuilder = new StringBuilder("SELECT `id`, `name`, `setid`, `sort_order`, `status`, `update_at` FROM tz_zt_images");
        List<Object> queryParams = new ArrayList<>();


        String wherestr = " WHERE";

        if (setid != null){
            countSqlBuilder.append(wherestr+" setid = ?");
            querySqlBuilder.append(wherestr+" setid = ?");
            countParams.add(setid);
            queryParams.add(setid);
        }

        if (status != null){
            if (setid != null){
                wherestr = " AND";
            }

            countSqlBuilder.append(wherestr+" status = ?");
            querySqlBuilder.append(wherestr+" status = ?");
            countParams.add(status);
            queryParams.add(status);
        }

        // 查询总条数
        String countSql = countSqlBuilder.toString();
        int total = jdbcTemplate.queryForObject(countSql, Integer.class, countParams.toArray());
        pageResult.setTotal(total);

        // 分页查询
        querySqlBuilder.append(" ORDER BY sort_order DESC LIMIT ?, ?");
        queryParams.add(offset);
        queryParams.add(pageSize);

        String querySql = querySqlBuilder.toString();
        List<ZtImages> images = jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(ZtImages.class), queryParams.toArray());
        pageResult.setData(images);

        return pageResult;
    }

    public List <ZtImages> getImagesBySetid(Long setid) {
        String sql = "SELECT * FROM tz_zt_images WHERE status = 1 AND setid = ? ORDER BY sort_order DESC ";
        List<ZtImages> result = jdbcTemplate.query(sql, new Object[]{setid}, new BeanPropertyRowMapper<>(ZtImages.class));
        return result != null ? result : new ArrayList<ZtImages>();
    }

    public ZtImages getImagesById(Long id) {
        String sql = "SELECT * FROM tz_zt_images WHERE id = ?";
        try {
            return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(ZtImages.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
    public int insertOrUpdateZtImages(ZtImages ztImages) {
        Long id = ztImages.getId();
        if (id != null) {
            // Update
            String sql = "UPDATE tz_zt_images SET ";
            List<String> updateFields = new ArrayList<>();
            List<Object> params = new ArrayList<>();

            if (ztImages.getSetid() != null) {
                updateFields.add("setid = ?");
                params.add(ztImages.getSetid());
            }
            if (ztImages.getImages() != null) {
                updateFields.add("images = ?");
                params.add(ztImages.getImages());
            }
            if (ztImages.getName() != null) {
                updateFields.add("name = ?");
                params.add(ztImages.getName());
            }
            if (ztImages.getSort_order() != null) {
                updateFields.add("sort_order = ?");
                params.add(ztImages.getSort_order());
            }
            if (ztImages.getStatus() != null) {
                updateFields.add("status = ?");
                params.add(ztImages.getStatus());
            }
            sql += String.join(", ", updateFields);
            sql += " WHERE id = ?";

            // 添加 id 参数
            params.add(id);

            jdbcTemplate.update(sql, params.toArray());
        } else {
            // Insert
            String sql = "INSERT INTO tz_zt_images (setid,name, images,sort_order,status) VALUES (?, ?, ?, ?, ?)";

            Object[] params = new Object[] {
                    ztImages.getSetid(),
                    ztImages.getName(),
                    ztImages.getImages(),
                    ztImages.getSort_order(),
                    ztImages.getStatus()
            };

            jdbcTemplate.update(sql, params);
        }
        return 1;
    }

    public Integer deleteImages(Integer id) {
        String sql = "DELETE FROM tz_zt_images WHERE id = ?";
        jdbcTemplate.update(sql, id);
        return 1;
    }

}
